// index.js
// 圆盘获取应用实例
//const app = getApp()

var canOnePointMove = false;

var pan1onePoint = { x: 0,y: 0};
var pan2onePoint = { x: 0,y: 0};
var pan3onePoint = { x: 0,y: 0};
var pan4onePoint = { x: 0,y: 0};

var pan1twoPoint = {x1: 0,y1: 0,x2: 0,y2: 0};
var pan2twoPoint = {x1: 0,y1: 0,x2: 0,y2: 0};
var pan3twoPoint = {x1: 0,y1: 0,x2: 0,y2: 0};
var pan4twoPoint = {x1: 0,y1: 0,x2: 0,y2: 0};


Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tips: {
      type: Array,
      value: [],
      observer: function (n) {
        if (n.length) {
          n.forEach(item => {
            if (item.type == 'warn') {
              item.w = item.tip_content.length * 40;
            } else {
              item.w = item.btn.length * 120;
            }

          })
          this.setData({
            tipsArr: n
          })
        }
      }
    },
    callbackFn: {
      type: Function,
      value: () => {}
    },
    tip0: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip0: n
          })
        }
      }
    },
    tip1: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip1: n
          })
        }
      }
    },
    tip2: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip2: n
          })
        }
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    width: 0,
    height: 0,
    left: 410,
    top: 300,
    msg: '',
      src1: 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/upload/202203101546337874.png',
      scale1: 1,
      rotate1: 0,
    count:0,
 },
 /**
  * 页面加载完成执行
  */
 lifetimes:{
  ready(){
    var that =this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          //buttonTop1:buttonTop1,//这里定义按钮的初始位置
          //buttonLeft:res.windowWidth*0.70,//这里定义按钮的初始位置
        })
      }
    })
    

  },
  attached: function() {
    // 在组件实例进入页面节点树时执行
  },
  detached: function() {
    // 在组件实例被从页面节点树移除时执行
  },
},
 //组件所在页面的生命周期
 pageLifetimes: {
  show: function() {
    // 页面被展示
  },
  hide: function() {
    // 页面被隐藏
  },
  resize: function(size) {
    // 页面尺寸变化
  }
},
   /**
   * 组件的方法列表
   */
  methods: {
 // 关闭上拉加载
  onReachBottom: function() {
 //return
 },
  bindload: function(e) {
 var that = this
 var width = e.detail.width
 var height = e.detail.height
 if (width > 750) {
      height = 750 * height / width
      width = 750
 }
 if (height > 1200) {
      width = 1200 * width / height
      height = 1200
 }
    that.setData({
      width: width,
      height: height
 })
 },

pantouchstart: function(e) {
 var that = this
 if (e.touches.length < 2) {
      canOnePointMove = true
      pan1onePoint.x = e.touches[0].pageX * 2
      pan1onePoint.y = e.touches[0].pageY * 2
 }else {
  pan1twoPoint.x1 = e.touches[0].pageX * 2
  pan1twoPoint.y1 = e.touches[0].pageY * 2
  pan1twoPoint.x2 = e.touches[1].pageX * 2
  pan1twoPoint.y2 = e.touches[1].pageY * 2
 }
 },

pantouchmove: function(e){
 var that = this
 if (e.touches.length < 2 && canOnePointMove) {
 var onePointDiffX = e.touches[0].pageX * 2 - pan1onePoint.x
 var onePointDiffY = e.touches[0].pageY * 2 - pan1onePoint.y
 var left = that.data.left + onePointDiffX;
 var top = that.data.top + onePointDiffY;
      that.setData({
        msg: '单点移动 left='+left+' top='+top,
        //left: left,
        //top: top,
      })
 pan1onePoint.x = e.touches[0].pageX * 2
 pan1onePoint.y = e.touches[0].pageY * 2
 }
 else if (e.touches.length > 1) {
   
 var preTwoPoint = JSON.parse(JSON.stringify(pan1twoPoint))
 pan1twoPoint.x1 = e.touches[0].pageX * 2
 pan1twoPoint.y1 = e.touches[0].pageY * 2
 pan1twoPoint.x2 = e.touches[1].pageX * 2
 pan1twoPoint.y2 = e.touches[1].pageY * 2
 // 计算角度，旋转(优先)
 var perAngle = Math.atan((preTwoPoint.y1 - preTwoPoint.y2)/(preTwoPoint.x1 - preTwoPoint.x2))*180/Math.PI
 var curAngle = Math.atan((pan1twoPoint.y1 - pan1twoPoint.y2)/(pan1twoPoint.x1 - pan1twoPoint.x2))*180/Math.PI
 if (Math.abs(perAngle - curAngle) > 1) {
      console.log('开始旋转....')
      var data1 = that.data.rotate1 + (curAngle - perAngle);
      if(data1 >26){
        data1 = 26
       }
       if(data1 <-26){
        data1 = -26
       }
        that.setData({
          msg: '旋转=== '+data1,
          rotate1: data1
        })
        if(data1==26){
          wx.showToast({
            title: '操作成功！',  // 标题
            icon: 'success',   // 图标类型，默认success
            duration: 1500   // 提示窗停留时间，默认1500ms
          })
        }
        console.log('旋转....'+that.data.rotate1)
 }else {
 // 计算距离，缩放
 var preDistance = Math.sqrt(Math.pow((preTwoPoint.x1 - preTwoPoint.x2), 2) + Math.pow((preTwoPoint.y1 - preTwoPoint.y2), 2))
 var curDistance = Math.sqrt(Math.pow((pan1twoPoint.x1 - pan1twoPoint.x2), 2) + Math.pow((pan1twoPoint.y1 - pan1twoPoint.y2), 2))
        that.setData({
          //msg: '缩放',
          //scale: that.data.q_pan1.scale + (curDistance - preDistance) * 0.005
 })
 }
 }
 },

 pantouchend: function(e) {
  var that = this
     canOnePointMove = false
  },
  /*
     * 内部私有方法建议以下划线开头
     * triggerEvent 用于触发事件
     */
    _okEvent(){
      //触发取消回调
      var that = this;
      console.log('触发取消回调')
      var isOk = 0;
      if(that.data.rotate1==26){
        isOk = 1;
      }
      return isOk;
      //this.triggerEvent("cancelEvent")
    },

 
    }
})